package com.hello.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hello.constant.MessageConstant;
import com.hello.dto.UserDTO;
import com.hello.dto.UserLoginDTO;
import com.hello.entity.User;
import com.hello.mapper.UserMapper;
import com.hello.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public boolean addUser(UserDTO userDTO) {
        User user = new User();
        //对象属性拷贝
        BeanUtils.copyProperties(userDTO,user);
        int num = baseMapper.insert(user);
        return num>0?true:false;

    }

    @Override
    public boolean delUserById(Long id) {
        int num = baseMapper.deleteById(id);
        return num>0?true:false;
    }

    @Override
    public List<UserDTO> queryUserList(){
        List<UserDTO> userDTOS = new ArrayList<>();
        List<User> list = lambdaQuery().list();
        for(User user:list){
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user,userDTO);
            userDTOS.add(userDTO);
        }
        return userDTOS;
    }

    @Override
    public UserDTO searchById(Long id){
        UserDTO  userDTO = new UserDTO();
        User user = baseMapper.selectById(id);
        BeanUtils.copyProperties(user,userDTO);
        log.info("Search by ID success:{}",userDTO);

        return userDTO;
    }
    @Override
    public boolean updateUser(UserDTO userDTO){
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        int num = baseMapper.updateById( user);
        return num>0?true:false;
    }
    @Override
    public UserDTO searchByName(String name){
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .select("*")
                .eq("username",name);
        User user = baseMapper.selectOne(wrapper);
//        if(user!=null) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            return userDTO;


    }
    @Override
    public  Integer login(UserLoginDTO userLoginDTO) throws AccountNotFoundException{
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .select("*")
                .eq("username",username);
        User user = baseMapper.selectOne(wrapper);
        if(user == null){
            return 1;
            //throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //对前端传来的明文密码进行md5加密处理 ？加密之后密码无法匹配
        //password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(user.getPassword())){
            return 2;
            //throw new AccountNotFoundException(MessageConstant.PASSWORD_ERROR);
        }
        return 0;
    }


}
